package com.sq.partner.manager.service.impl;

import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.*;
import com.sq.partner.manager.mybatis.model.*;
import com.sq.partner.manager.service.IFuelGasPolicyListService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.excel.ExcelEntity;
import com.sq.partner.manager.util.excel.ExportExcelBuilderTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FuelGasPolicyListServiceImpl implements IFuelGasPolicyListService {


    @Resource
    ExportExcelBuilderTool exportExcelBuilderTool;

    @Resource
    private IGatewayPolicyDao policyDao;

    @Resource
    private IGatewayChannelInfoConfigDao channelInfoConfigDao;
    @Resource
    private IGatewayNocarBaseDao  gatewayNocarBaseDao;
    @Resource
    private IGatewayEmailDao gatewayEmailDao;

    @Resource
    private IGatewaySMSDao gatewaySMSDao;
    @Override
    public Page queryFuelGasPolicyPageByCriteria(Page page) throws Exception {
        Page page1 = policyDao.queryFuelGasPolicyPageByCriteria(page);
        return page1;
    }

    @Override
    public List<Map<String, Object>> queryFuelGasPolicyListByCriteria(
            Map<String, Object> map) throws Exception {
        return policyDao.queryFuelGasPolicyListByCriteria(map);
    }

    @Override
    public void downloadFuelGasPolicyFile(
            List<Map<String, Object>> nocarPolicyList,
            HttpServletResponse response) throws Exception {

        ExcelEntity excel = new ExcelEntity();
        excel.setResults(nocarPolicyList);
        excel.setFileName("NocarPolicyInfo");
        excel.setTitles("保单号,投保人,用户号,录入人工号,录入人姓名,投保单位,投保单位子机构,出单机构,投保方案,经办人,经办人电话,人数,保费,保单提交日期,起保日期,终保日期,单证号,员工编号,状态");
        excel.setTitleKeys("POLNO,POLICY_HOLDER,USER_NO,REFERRER_CODE,REFERRER,CHANNEL_NAME,SUB_CHANNEL_NAME,DPT_CDE,PLAN_NAME,PURVEYOR_NAME,PURVEYOR_TEL,BBR_NUM,PREMIUM,INSERT_TIME,BEGIN_DATE,END_DATE,CARD_NO,SPICAL1,STATE_ID");
        excel.setColumns("35,10,15,20,20,25,25,60,30,15,15,10,15,23,23,23,25,15,10");

        exportExcelBuilderTool.buildExcel(response,excel);
    }

    @Override
    public List<Map> queryChannelListOfNocar(
            GatewayChannelInfo channelInfo,String flag) throws Exception {
        return channelInfoConfigDao.queryChannelInfoList(channelInfo,flag);
    }

    @Override
    public GatewayPolicy queryPolicyInfoByPolNo(String polNo) throws Exception {
        // TODO Auto-generated method stub
        return policyDao.queryNocarPolicyInfoByPolNo(polNo);
    }

    @Override
    public GatewayNocarBase queryNocarBaseByPolicyId(String policyId)
            throws Exception {
        return gatewayNocarBaseDao.queryNocarBaseByPolicyId(policyId);
    }

    @Override
    public Result addEmailReSend(String polNo, String email) throws Exception {
        /**查询已发送过的邮件*/
        List<GatewayEmail> emails=gatewayEmailDao.queryEmailByPolNo(polNo);
        if(emails==null||emails.size()==0){
            return Result.error("此单没有历史邮件发送记录");
        }
        GatewayEmail emailDto=emails.get(0);
        emailDto.setEmail(email);
        if(emailDto.getEmailId().contains("CF")){
            emailDto.setEmailId("CF"+(Integer.parseInt(emailDto.getEmailId().replace("CF", ""))+emails.size()));
        }else{
            emailDto.setEmailId("CF"+emailDto.getEmailId()+emails.size());
        }

        if(gatewayEmailDao.addGateWayEmail(emailDto)){
            return Result.success("发送成功");
        };
        return Result.error("发送失败");
    }

    @Override
    public Result addSMSReSend(String polNo, String phoneNo) throws Exception {
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat format1=new SimpleDateFormat("yyyyMMddHHmmssSSS");
        /**查询已发送过的短信*/
        List<GatewaySMS> emss=gatewaySMSDao.querySMSByPolNo(polNo);
        if(emss==null||emss.size()==0){
            return Result.error("此单没有历史短信发送记录");
        }
        GatewaySMS ems=emss.get(0);
        if(ems.getSmsId().contains("CF")){
            ems.setSmsId("CF"+(Integer.parseInt(ems.getSmsId().replace("CF", ""))+emss.size()));
        }else{
            ems.setSmsId("CF"+ems.getSmsId()+emss.size());
        }
        ems.setPhoneCode(phoneNo);
        ems.setSendTime(format.format(new Date()));
        ems.setId("CF"+format1.format(new Date())+"560709");
        if(gatewaySMSDao.addGateWaySMS(ems)){
            return Result.success("发送成功");
        };
        return Result.error("发送失败");
    }

    @Override
    public Map<String, Object> queryFuelGasPolicyAll(Map<String, String> map) throws Exception {
        return policyDao.queryFuelGasPolicyAll(map);
    }

}
